package com.dindang.maintenance.mvp.ui.adapter.custom;

import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.dindang.maintenance.R;
import com.dindang.maintenance.app.utils.TextTransformUtils;
import com.dindang.maintenance.mvp.model.entity.Repair;
import com.longxi.gamer_base.utils.TextViewUtils;

import java.util.List;

/**评论 数据适配器*/
public class ProgressViewAdapter extends RecyclerView.Adapter<ProgressViewAdapter.ViewHolder> {

    String[] status = {"追记", "派单", "接单", "重新派单", "暂停", "继续", "完成", "已回访", "评价", "撤销"};
    String[] repairType = {"","自修","自修多人协助","外派"};
    List<Repair.FlowDetail.FlowList> list;
//    状态(1:待派单2:待接单3:待完成4:暂停中5:待回访6:已回访9已撤销)
    public ProgressViewAdapter(List<Repair.FlowDetail.FlowList> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.maintenance_item_progress, parent, false);
        ProgressViewAdapter.ViewHolder viewHolder = new ProgressViewAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Repair.FlowDetail.FlowList flow = list.get(position);

        if(!TextUtils.isEmpty(flow.d_create_date)){
            holder.maintenance_tv_time.setText(flow.d_create_date);
        }

        if(list.size() == 1){
            holder.view_dotted_line_top.setVisibility(View.GONE);
            holder.iv_progress.setImageResource(R.mipmap.maintenance_ic_progress_now);
            holder.view_dotted_line_bottom.setVisibility(View.GONE);
            holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
            holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);
        }else{
            if(position == 0){
                holder.view_dotted_line_top.setVisibility(View.GONE);
                holder.iv_progress.setImageResource(R.mipmap.maintenance_ic_progress_old);
                holder.view_dotted_line_bottom.setVisibility(View.VISIBLE);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.maintenance_color_blue));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_border_blue);
            }else if(position == list.size() - 1){
                holder.view_dotted_line_top.setVisibility(View.VISIBLE);
                holder.iv_progress.setImageResource(R.mipmap.maintenance_ic_progress_now);
                holder.view_dotted_line_bottom.setVisibility(View.GONE);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);
            }else{
                holder.view_dotted_line_top.setVisibility(View.VISIBLE);
                holder.iv_progress.setImageResource(R.mipmap.maintenance_ic_progress_old);
                holder.view_dotted_line_bottom.setVisibility(View.VISIBLE);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.maintenance_color_blue));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_border_blue);
            }
        }

        if(TextTransformUtils.string2Int(flow.c_status) < status.length){
            holder.tv_state.setText(status[TextTransformUtils.string2Int(flow.c_status)]);
        }

        StringBuffer contents = new StringBuffer();
        switch (flow.c_status) {
            case "0":// 追记
                holder.maintenance_tv_name.setText("追记人: " + flow.c_creator);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.maintenance_color_blue));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_border_blue);
                holder.maintenance_tv_contents.setText("● 追记描述: " + flow.c_remark);
                break;
            case "1":// 派单
                holder.maintenance_tv_name.setText("派单人: " + flow.c_creator);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);
                contents.append("● 处理方式: " + repairType[TextTransformUtils.string2Int(flow.c_type)]);
                contents.append("\n● 执行对象: ");
                for (Repair.FlowDetail.FlowList.Executors executor : flow.executors)
                    contents.append(executor.c_executor + " ");
                contents.append("\n● 到场时间: ");
                contents.append(flow.d_appoint_date);
                contents.append(" ");
                contents.append(flow.c_start_date);
                contents.append("至");
                contents.append(flow.c_end_date);
                contents.append("\n● 派单说明: " + flow.c_remark);

                holder.maintenance_tv_contents.setText(contents);
                break;
            case "2":// 接单
                holder.maintenance_tv_name.setText("接单人: " + flow.c_creator);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);
                contents.append("● 维修费用: " + flow.f_cost);
                contents.append("\n● 维修说明: " + flow.c_remark);
                holder.maintenance_tv_contents.setText(contents.toString());
                break;
            case "3":// 重新派单
                holder.maintenance_tv_name.setText("派单人: " + flow.c_creator);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);
                contents.append("● 处理方式: " + repairType[TextTransformUtils.string2Int(flow.c_type)]);
                contents.append("\n● 执行对象: ");
                for (Repair.FlowDetail.FlowList.Executors executor : flow.executors)
                    contents.append(executor.c_executor + " ");
                contents.append("\n● 到场时间: ");
                contents.append("\n● 派单说明: " + flow.c_remark);
                holder.maintenance_tv_contents.setText(contents.toString());
                break;
            case "4":// 暂停
                holder.maintenance_tv_name.setText("记录人: " + flow.c_creator);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);
                contents.append("● 暂停原因: " + flow.c_remark);
                holder.maintenance_tv_contents.setText(contents.toString());
                break;
            case "5":// 继续
                holder.maintenance_tv_name.setText("记录人: " + flow.c_creator);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);
                break;
            case "6":// 完成
                holder.maintenance_tv_name.setText("记录人: " + flow.c_creator);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);
                contents.append("● 完成情况: " + flow.c_remark);
                holder.maintenance_tv_contents.setText(contents.toString());
                break;
            case "7":// 已回访
                holder.maintenance_tv_name.setText("回访人: " + flow.c_creator);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);
                contents.append("● 回访情况: " + flow.c_remark);
                holder.maintenance_tv_contents.setText(contents.toString());
                break;
            case "8":// 评价
                holder.maintenance_tv_name.setText("记录人: " + flow.c_creator);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);

                holder.maintenance_tv_contents.setText(TextViewUtils.getStarText(holder.maintenance_tv_contents.getContext(), 3, 3, 3));
                break;
            case "9":// 撤销
                holder.maintenance_tv_name.setText("提交人: " + flow.c_creator);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);
                contents.append("● 撤销原因: " + flow.c_remark);
                holder.maintenance_tv_contents.setText(contents.toString());
                break;
            case "99":
                holder.maintenance_tv_name.setText("");
                holder.maintenance_tv_time.setText("");
                holder.maintenance_tv_contents.setText("");
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_red);
                holder.tv_state.setText(flow.status);
                break;
            case "100":// 提交
                holder.maintenance_tv_name.setText("提交人: " + flow.c_creator);
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.maintenance_bg_radio_10_blue);
                holder.tv_state.setText("提交");
                contents.append("● 方便上门时间: " + flow.c_remark);
                holder.maintenance_tv_contents.setText(contents.toString());
                break;
            default:
                holder.maintenance_tv_name.setText("");
                holder.maintenance_tv_time.setText("");
                holder.maintenance_tv_contents.setText("");
                holder.tv_state.setTextColor(holder.tv_state.getContext().getResources().getColor(R.color.white));
                holder.tv_state.setBackgroundResource(R.drawable.bg_radio_shape_white);
                holder.tv_state.setText(flow.c_status);
                break;
        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        ConstraintLayout maintenance_cl_maintenace;
        View view_dotted_line_top;
        ImageView iv_progress;
        View view_dotted_line_bottom;
        TextView tv_state;
        TextView maintenance_tv_time;
        TextView maintenance_tv_name;
        TextView maintenance_tv_contents;
        ViewHolder(View itemView) {
            super(itemView);
            maintenance_cl_maintenace = itemView.findViewById(R.id.maintenance_cl_maintenace);
            view_dotted_line_top = itemView.findViewById(R.id.maintenance_view_dotted_line_top);
            iv_progress = itemView.findViewById(R.id.maintenance_iv_progress);
            view_dotted_line_bottom = itemView.findViewById(R.id.maintenance_view_dotted_line_bottom);
            tv_state = itemView.findViewById(R.id.maintenance_tv_state);
            maintenance_tv_time = itemView.findViewById(R.id.maintenance_tv_time);
            maintenance_tv_name = itemView.findViewById(R.id.maintenance_tv_name);
            maintenance_tv_contents = itemView.findViewById(R.id.maintenance_tv_contents);
        }
    }

}